package org.snia.server.cfg;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * 读取系统配置文件信息
 * @author alpeai@126.com
 *
 */
public final class XmlConfigurationFactory {
	
	private final static String CONFIGURE_FILE_PATH = "WEB-INF/server-setting.xml";
	public static ConfigurationSetting cfg = null;
	
	//不允许构造创建对象
	private XmlConfigurationFactory(){}
	
	public static ConfigurationSetting buildConfiguration(){
		if(cfg == null){
			return configure(CONFIGURE_FILE_PATH);
		}
		return cfg;
	}
	
	private static ConfigurationSetting configure(String filename){
		XStream xs = new XStream(new DomDriver());
//		xs.alias("Container", ContainerMetaFile.class);
//		xs.alias("DataObject", DataObjectMetaFile.class); 
//		xs.alias("DataSlice", DataSliceMetaFile.class);
		
		FileInputStream fis = null;
		ConfigurationSetting cfg = null;
		try {
			fis = new FileInputStream(filename);
			cfg = (ConfigurationSetting)xs.fromXML(fis);
		} catch (FileNotFoundException ex) {
			return null;
		} finally {
			try {
				if(fis != null){
					fis.close();
				}
			} catch (Exception e) {
				//忽略
			}
		}
		return cfg;
//		buildClusterConfiguration(cfg);
		
	}
	
//	private static void buildClusterConfiguration(Configuration cfg){
//		ClusterConfiguration.ID = cfg.getClusterSetting().getId();
//		ClusterConfiguration.NAME = cfg.getClusterSetting().getName();
//		ClusterConfiguration.HOST = cfg.getClusterSetting().getHost();
////		ClusterConfiguration.TYPE = cfg.getClusterSetting().getType();//暂不支持
//		ClusterConfiguration.architecture = cfg.getClusterSetting().getArchitecture();
//	}
//	
//	private static void buildStorageConfiguration(Configuration cfg){
//		StorageConfiguration.ID = cfg.getClusterSetting().getId();
//		StorageConfiguration.NAME = cfg.getClusterSetting().getName();
//		StorageConfiguration.HOST = cfg.getClusterSetting().getHost();
////		ClusterConfiguration.TYPE = cfg.getClusterSetting().getType();//暂不支持
//		StorageConfiguration.architecture = cfg.getClusterSetting().getArchitecture();
//	}
}
